package ua.com.softway.db.batch;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BatchProcessorStatusServlet extends HttpServlet {
    private final BatchProcessor batchProcessor;

    public BatchProcessorStatusServlet(BatchProcessor batchProcessor) {
        this.batchProcessor = batchProcessor;
    }
    
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");

        PrintWriter writer = response.getWriter();

        writer.println("Batch processor: running " + (batchProcessor.currentJob == null ? "nothing" : batchProcessor.currentJob)
                + ", in queue " + batchProcessor.jobs.size() + " of " + (batchProcessor.jobs.size() + batchProcessor.jobs.remainingCapacity()));
        
        for (BatchJob<?> job : batchProcessor.jobs)
            writer.println(job.toString());

        writer.flush();
    }
}
